#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: tags.py
@contact: zwb15083976291@163.com
@time: 2022/02/25
"""

from common.models import BlogTag
from common.serializers.serializer import TagsSerializer
from utils.responses import CommonResponse, CommonAPIResponseStatus
from utils.views import BaseGenericAPIView


class TagsListView(BaseGenericAPIView):
    serializer_class = TagsSerializer
    diff_methods_request_params = dict(get=dict(optional=['id']))

    def get(self, request):
        ret, message = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=message)
        many = True
        if self.query_data.get('id', None) is not None:
            query_set = BlogTag.objects.filter(id=self.query_data.get('id')).first()
            many = False
        else:
            query_set = BlogTag.objects.all()
        serial_fields = ['id', 'tag_name']
        return CommonResponse(data=TagsSerializer(instance=query_set, many=many, serial_fields=serial_fields).data)
